import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';

import {
  ModalController,
  ViewController
} from 'ionic-angular';

import { Common } from '../../../../providers/common';

import _ from 'lodash';
import { ContactChoosePage } from "../../../contact/choose/choose.component";

@Component({
  selector: 'page-irp-pool-query',
  templateUrl: './query.html'
})
export class IrpPoolQueryPage {
  moa_page_name: any = "page-irp-pool-query";
  moa_page_explain: any = "股票池管理 - 高级查询";

  enddate;
  startdate;
  condition = null;

  defaultEmpByTree = [];
  adjustmen;

  constructor(
    public modalCtrl: ModalController,
    public viewCtrl: ViewController,
    public storage: Storage,
    public common: Common) {
    /* let d = new Date();
    let m = d.getMonth() + 1;
    this.enddate = d.getFullYear() + "-" + (m > 9 ? m : "0" + m) + "-" + (d.getDate() > 9 ? d.getDate() : "0" + d.getDate());
    this.startdate = d.getFullYear() + "-01-01"; */

    this.initCondition();
    this.storage.get("advancedQueryCondition")
      .then(data => {
        console.log("test" + data);
        if (data != null) {
          this.condition = data;
        }
      });
  }

  cancel() {
    this.viewCtrl.dismiss("close");
  }

  chooseEmp() {
    console.log("##authorization.detail.chooseEmpnames 选择外出人员");
    let that = this;
    let modal = this.modalCtrl.create(ContactChoosePage, {
      selected: this.defaultEmpByTree,
      isSelectUser: true,
      isSelectOne: true
    });

    modal.present();
    modal.onDidDismiss((data: any[]) => {
      console.info(data);
      if (data) {
        that.defaultEmpByTree = data;
        this.condition.emp = _.toString(_.map(data, "name"));
        that.adjustmen = _.toString(_.map(data, "name"));
      }
    });
  }

  /**
   * 初始化查询条件
   */
  initCondition() {
    this.condition = {
      "emp": "",
      "startdate": (this.common.getDate(-365)),
      "enddate": (this.common.getNowFormatDate())
    }
  }

  search() {
    this.viewCtrl.dismiss("true");
  }
}


